package com.neuedu.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.entity.Department;
import com.neuedu.mapper.DepartmentMapper;
import com.neuedu.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 科室表 服务实现类
 * </p>
 *
 * @author LiYang
 * @since 2024-11-05
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    @Override // 模糊查询 pageNo:页码，pageSize:每页数据量，name:模糊查询字段
    public IPage<Department> list(Integer pageNo, Integer pageSize, String name) {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        wrapper.orderByDesc("id");
        return this.page(new Page<>(pageNo, pageSize), wrapper);
    }

    @Override
    @CacheEvict(value = "admin", key = "'dept'") // 新增数据 删除缓存
    public Boolean add(String code, String name, String type) {
        return this.save(new Department(code, name, type));
    }
    @Override
    public Boolean check(String field, String value,Long id) {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq(field, value);
        if (id != null){
            wrapper.ne("id", id);
        }
        return this.count(wrapper) == 0;
    }

    @Override
    @CacheEvict(value = "admin", key = "'dept'") // 删除数据 删除缓存
    public Boolean del(Long id, Boolean delmark) {
        return this.updateById(new Department(id, delmark));
    }

    @Override
    @CacheEvict(value = "admin", key = "'dept'") // 更新数据 删除缓存
    public Boolean update(Long id, String code, String name, String type) {
        return this.updateById(new Department(id, code, name, type));
    }
    @Override
    @Cacheable(value = "admin", key = "'dept'") // 缓存数据 key为dept
    public List<Department> getActive() {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq("delmark", 1);
        return this.list(wrapper);
    }
    @Override
    @Cacheable(value = "admin", key = "'register::dept'")
    public List<Department> getRegisterDept() {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq("type", "门诊")
                .eq("delmark", 1);
        return this.list(wrapper);
    }
}
